package simulation;

import streaming.Overlay;
import streaming.StreamingConfig;

public class PeriodicRemoval implements EventHandler {

	Overlay overlay;
	Simulator sim;
	
	public PeriodicRemoval(Overlay overlay){
		this.overlay = overlay;
		this.sim = Simulator.getInstance();
	}
	
	public void addEvent() {

		double time = this.getSim().getTime() + this.getInterval();
		
		// Truncate double value in at most 2 decimals
		time = Math.round(time * 100) / 100d;
		
//		System.out.println("Remocao: " + time);
		
		if (Math.abs(time - Math.round(time)) < 0.00001)
			time = Math.round(time);
		this.getSim().addEvent(time, this);
	}
	
	/**
	 * @brief This event, with time as a parameter, was built to generate events outside this class, more especifically the
	 * events of removal calculated by probabilistic distributions
	 * @param time
	 */
	public void addEvent(double time){
		if (Math.abs(time - Math.round(time)) < 0.00001)
			time = Math.round(time);
		
		this.getSim().addEvent(time, this);
	}
	
	public void run(double time) {
		this.overlay.removeHostByEvent();
	}

	public Simulator getSim() {
		return sim;
	}

	public void setSim(Simulator sim) {
		this.sim = sim;
	}
	
	public double getInterval() {
		return StreamingConfig.DYNAMIC_HOST_REMOVAL_INTERVAL;
	}
	
}
